// packageA/pages/withdraw/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        name: '',
        phone: '',
        statusIndex: 0,
        statusOptions: [
          { text: '全部状态', value: -1 },
          { text: '未通过', value: 0 },
          { text: '已审核', value: 1 }
        ],
        withdrawList: [
          {
            id: '10001',
            name: '张三',
            phone: '13800138001',
            time: '2023-05-10 14:30',
            amount: 500,
            status: 0
          },
          {
            id: '10002',
            name: '李四',
            phone: '13800138002',
            time: '2023-05-11 09:15',
            amount: 1200,
            status: 1
          },
          {
            id: '10003',
            name: '王五',
            phone: '13800138003',
            time: '2023-05-11 16:45',
            amount: 800,
            status: 0
          }
        ],
        filteredList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            filteredList: this.data.withdrawList
          });
    },
    inputName: function(e) {
        this.setData({
          name: e.detail.value
        });
      },
    
      inputPhone: function(e) {
        this.setData({
          phone: e.detail.value
        });
      },
    
      statusChange: function(e) {
        this.setData({
          statusIndex: e.detail.value
        });
      },
    
      searchWithdraw: function() {
        const { name, phone, statusIndex, statusOptions, withdrawList } = this.data;
        const statusValue = statusOptions[statusIndex].value;
        
        let filteredList = withdrawList;
        
        if (name) {
          filteredList = filteredList.filter(item => item.name.includes(name));
        }
        
        if (phone) {
          filteredList = filteredList.filter(item => item.phone.includes(phone));
        }
        
        if (statusValue !== -1) {
          filteredList = filteredList.filter(item => item.status === statusValue);
        }
        
        this.setData({
          filteredList: filteredList
        });
        
        if (filteredList.length === 0) {
          wx.showToast({
            title: '未找到匹配记录',
            icon: 'none'
          });
        }
      },
    
      handleReview: function(e) {
        const id = e.currentTarget.dataset.id;
        wx.showModal({
          title: '审核操作',
          content: '确定要通过此提现申请吗？',
          success: (res) => {
            if (res.confirm) {
              this.approveWithdraw(id);
            }
          }
        });
      },
    
      approveWithdraw: function(id) {
        wx.showLoading({
          title: '处理中...',
        });
        
        setTimeout(() => {
          const newList = this.data.withdrawList.map(item => {
            if (item.id === id) {
              return { ...item, status: 1 };
            }
            return item;
          });
          
          this.setData({
            withdrawList: newList,
            filteredList: newList.filter(item => {
              const { name, phone, statusIndex, statusOptions } = this.data;
              const statusValue = statusOptions[statusIndex].value;
              return (!name || item.name.includes(name)) &&
                     (!phone || item.phone.includes(phone)) &&
                     (statusValue === -1 || item.status === statusValue);
            })
          });
          
          wx.hideLoading();
          wx.showToast({
            title: '审核通过',
            icon: 'success'
          });
        }, 1000);
      },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})